#!/usr/bin/env bash
#
# Summary: Set or show the global Java version
#
# Usage: jenv global <version>
#
# Sets the global Java version. You can override the global version at
# any time by setting a directory-specific version with `jenv local'
# or by setting the `JENV_VERSION' environment variable.
#
# <version> should be a string matching a Java version known to jenv.
# The special version string `system' will use your default system Java.
# Run `jenv versions' for a list of available Java versions.

set -e
[ -n "$JENV_DEBUG" ] && set -x

# Provide jenv completions
if [ "$1" = "--complete" ]; then
  echo --unset
  echo system
  exec jenv-versions --bare
fi

JENV_VERSION="$1"
JENV_VERSION_FILE="${JENV_ROOT}/version"


if [ "$JENV_VERSION" = "--unset" ]; then
  rm -f "${JENV_ROOT}/version"
elif [ -n "$JENV_VERSION" ]; then
  jenv-version-file-write "$JENV_VERSION_FILE" "$JENV_VERSION"
else
  jenv-version-file-read "$JENV_VERSION_FILE" ||
  jenv-version-file-read "${JENV_ROOT}/global" ||
  jenv-version-file-read "${JENV_ROOT}/default" ||
  echo system
fi
